package com.weizai.carshop.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weizai.carshop.mapper.SideShowMapper;
import com.weizai.carshop.model.dto.SetSideShowDto;
import com.weizai.carshop.model.entiity.SlideshowEntity;
import com.weizai.carshop.service.SideShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weizai.carshop.common.result.PageResult;

import java.time.LocalDateTime;

@Service
public class SideShowServiceImpl extends ServiceImpl<SideShowMapper,SlideshowEntity> implements SideShowService {

    @Autowired
    private SideShowMapper sideShowMapper;

    /**
     * 查询轮播图列表
     * @param page
     * @param pageSize
     */
    @Override
    public PageResult getSideShowList(Integer page, Integer pageSize) {
        // 构建分页对象
        Page<SlideshowEntity> res = this.lambdaQuery().page(new Page<>(page, pageSize));
        // 执行分页查询
       return new PageResult(res.getTotal(), res.getRecords());
    }

    /**
     * 修改和新增轮播图
     * @param setSideShowDto
     */
    @Override
    public void setSwiper(SetSideShowDto setSideShowDto) {
        SlideshowEntity slideshowEntity = BeanUtil.copyProperties(setSideShowDto, SlideshowEntity.class);
        if (slideshowEntity.getId() == null){
            // 新增功能
            slideshowEntity.setCreateTime(LocalDateTime.now());
            slideshowEntity.setUpdateTime(LocalDateTime.now());
        }else{
            // 编辑功能
            slideshowEntity.setUpdateTime(LocalDateTime.now());
        }

        this.saveOrUpdate(slideshowEntity);
    }
}
